//
//  RegistViewController.swift
//  甜言蜜语
//
//  Created by 王剑伟 on 16/7/4.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class RegistViewController: UIViewController {
    
    @IBOutlet weak var phone: UITextField!
   
    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var identiferCode: UITextField!
    @IBOutlet weak var smsBtn: UIButton!
    
    @IBOutlet weak var phoneView: UIView!
   
    @IBOutlet weak var passwordView: UIView!
    @IBOutlet weak var idenView: UIView!
    @IBOutlet weak var eyes: UIButton!
    
    var typeID: String?
    var type: String = ""
    var modelList: regiestInfo?{
        didSet{
            
        }
    }
    var model: loginInfo?{
        didSet{
            
        }
    }
    var timeTick: Int = 0
    var timer: NSTimer?
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
        self.phoneView.layer.masksToBounds = true
        self.phoneView.layer.borderWidth = 1
        
       
        
        self.passwordView.layer.masksToBounds = true
        self.passwordView.layer.borderWidth = 1
        
        self.idenView.layer.masksToBounds = true
        self.idenView.layer.borderWidth = 1
        self.password.secureTextEntry = true

       
    }
    override func viewWillDisappear(animated: Bool) {
        SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    override func viewWillAppear(animated: Bool) {
        self.timer?.fireDate = NSDate.distantPast()
        super.viewWillAppear(animated)
    }
    
    override func viewDidDisappear(animated: Bool) {
        self.timer?.fireDate = NSDate.distantFuture()
        super.viewDidDisappear(animated)
    }
    //MARK:target
    @IBAction func eyesBtnSelected(sender: UIButton){
        sender.selected = !sender.selected
        if sender.selected{// 显示密码
           self.password.text = ""
           self.password.clearButtonMode = UITextFieldViewMode.Never
           self.password.secureTextEntry = false
           self.eyes.setImage(UIImage(named: "regist_eyes_selected"), forState: UIControlState.Normal)
        }else{//秘密输入
             self.password.text = ""
           self.password.clearButtonMode = UITextFieldViewMode.Never
           self.password.secureTextEntry = true
           self.eyes.setImage(UIImage(named: "regist_eyes"), forState: UIControlState.Normal)
        }
        
    }
    func sendIdfentiCode() {
        self.smsBtn.backgroundColor = UIColor.lightGrayColor()
        timeTick = 61
        //60秒倒计时
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(self.timeFireMethod), userInfo: nil, repeats: true)
        self.smsBtn.enabled = false
    }
    
    func timeFireMethod() {
        timeTick -= 1
        if timeTick == 0 {
            timer!.invalidate()
            //停止
            //self.timer = 0
            self.smsBtn.enabled = true
            self.smsBtn.setTitle("重新获取", forState: .Normal)
            self.smsBtn.backgroundColor = UIColor.lightGrayColor()
        }
        else {
            let str: String = "\(timeTick)"
            self.smsBtn.setTitle("\(str)秒", forState: .Normal)
        }
    }
    
    @IBAction func smsBtnClicked(sender: UIButton){
        if self.phone.text == ""{
            SVProgressHUD.showErrorWithStatus("请输入手机号码")
            
            return
        }else{
            DataTool.sendMessageCode(self.phone.text!, completionHandler: { (newModel) in
                guard newModel != nil else {
                    
                    return
                }
                self.modelList = newModel
                if self.modelList?.msg == "发送成功"{
                  self.sendIdfentiCode()
               SVProgressHUD.showSuccessWithStatus(self.modelList!.msg)
                   
                }else{
                   
                    SVProgressHUD.showErrorWithStatus(self.modelList!.msg)
                    return
                }
                
            })
        }
        
    }
    
    @IBAction func regiestBtnClicked(sender: UIButton){
        if self.phone.text == ""{
            SVProgressHUD.showErrorWithStatus("请输入手机号码")
            
            return
        }
        if self.password.text == ""{
            SVProgressHUD.showErrorWithStatus("请输入密码")
           
            return
        }
        if self.identiferCode.text == ""{
            SVProgressHUD.showErrorWithStatus("请输入验证码")
            
            return
        }else{
            
//           // var jsonData = ["code": self.identiferCode.text!, "phone": self.phone.text!, "pass": self.password.text!]
//            
//            if self.type == "qq"{
//                //jsonData = ["code": self.identiferCode.text!, "phone": self.phone.text!, "pass": self.password.text!, "qq": self.typeID!]
//                
//            }
//            
//            if self.type == "weixin"{
//                //jsonData = ["code": self.identiferCode.text!, "phone": self.phone.text!, "pass": self.password.text!, "weixin": self.typeID!]
//            }
//            
//            if self.type == "sina"{
//                //jsonData = ["code": self.identiferCode.text!, "phone": self.phone.text!, "pass": self.password.text!, "sina": self.typeID!]
//            }
        NSUserDefaults.standardUserDefaults().setObject(self.phone.text, forKey: "k_UD_user")
            
        NSUserDefaults.standardUserDefaults().setObject(self.password.text, forKey: "k_UD_pass")
            
        NSUserDefaults.standardUserDefaults().synchronize()

            let vc = self.storyboard?.instantiateViewControllerWithIdentifier("ChooseTypeTableViewController") as! ChooseTypeTableViewController
            vc.phone = self.phone.text!
            vc.passWord = self.password.text!
            
            vc.type = self.type
            if self.type != "" {
                 vc.identyCode = self.typeID
            }
            self.navigationController?.pushViewController(vc, animated: true)


//            DataTool.regiest(jsonData, completionHandler: { (newModel) in
//                    guard newModel != nil else {
//                        
//                        return
//                }
//                self.modelList = newModel
//                if self.modelList?.msg == "注册成功"{
//                
//                    SVProgressHUD.showSuccessWithStatus(self.modelList!.msg)
//                    NSUserDefaults.standardUserDefaults().setObject(self.phone.text, forKey: "k_UD_user")
//                    
//                    NSUserDefaults.standardUserDefaults().setObject(self.password.text, forKey: "k_UD_pass")
//                    
//                    NSUserDefaults.standardUserDefaults().synchronize()
//                    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("ChooseTypeTableViewController") as! ChooseTypeTableViewController
//                    
//                    self.navigationController?.pushViewController(vc, animated: true)
//
//                    
//                }else{
//                     
//                    SVProgressHUD.showErrorWithStatus(self.modelList!.msg)
//                    return
//                }
//               
//            })
        }
    }
    
    func loginAction(){
        let token = NSUserDefaults.standardUserDefaults().stringForKey("k_UD_token")
        DataTool.login(self.phone.text!, pass: self.password.text!,token: token, completionHandler:{ (newModel) in
            guard let newDataes = newModel else {
                return
            }
            print(newDataes)
            self.model = newDataes
            
            if self.model?.msg == "登陆成功"{
                
            SVProgressHUD.showSuccessWithStatus(self.modelList?.msg)
            NSUserDefaults.standardUserDefaults().setObject(self.phone.text, forKey: "k_UD_user")
                
            NSUserDefaults.standardUserDefaults().setObject(self.password.text, forKey: "k_UD_pass")
                
                
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "k_UD_isLogin")
                
                NSUserDefaults.standardUserDefaults().synchronize()
                
        
            let vc = self.storyboard?.instantiateViewControllerWithIdentifier("ChooseTypeTableViewController") as! ChooseTypeTableViewController
                
            self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                SVProgressHUD.showErrorWithStatus(self.modelList?.msg)
                
                return
            }
            
        })

    }

}
